package com.xc.ksxt.service;

import com.google.common.collect.Lists;
import com.xc.common.tool.Helper;
import com.xc.ksxt.entity.ChoiceEntity;
import com.xc.ksxt.vo.ChoiceVO;
import com.xc.common.object.PageInfo;
import com.xc.db.tables.Choice;
import com.xc.db.tables.records.ChoiceRecord;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SelectForUpdateStep;
import org.jooq.SelectSeekStepN;
import org.jooq.SortField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ChoiceService {
    @Autowired
    private DSLContext dsl;
    private Choice CHOICE = Choice.CHOICE.as("CHOICE");
    public Choice getTable() {
        return CHOICE;
    }
    public Result<Record> select(Condition whr, PageInfo<ChoiceEntity> pageInfo, SortField<?>... orderby) {
        SelectSeekStepN<Record> seek = dsl.select().from(CHOICE).where(whr).orderBy(orderby);
        Result<Record> result = null;
        if (pageInfo != null) {
            SelectForUpdateStep<Record> su = seek.limit(pageInfo.getLimit()).offset(pageInfo.getBeginNum());
            result = su.fetch();
        } else {
            result = seek.fetch();
        }
        if (pageInfo != null) {
            pageInfo.setResult(result);
            pageInfo.setList(result.into(ChoiceEntity.class));
        }
        return result;
    }
    public Result<Record> selectVO(Condition whr, PageInfo<ChoiceVO> pageInfo, SortField<?>... orderby) {
        SelectSeekStepN<Record> seek = dsl.select().from(CHOICE).where(whr).orderBy(orderby);
        Result<Record> result = null;
        if (pageInfo != null) {
            SelectForUpdateStep<Record> su = seek.limit(pageInfo.getLimit()).offset(pageInfo.getBeginNum());
            result = su.fetch();
        } else {
            result = seek.fetch();
        }
        if (pageInfo != null) {
            pageInfo.setResult(result);
            pageInfo.setList(result.into(ChoiceVO.class));
        }
        return result;
    }

    public List<ChoiceEntity> select(Condition whr, SortField<?>... orderby) {
        Result<Record> result = dsl.select().from(CHOICE).where(whr).orderBy(orderby).fetch();
        List<ChoiceEntity> list = Lists.newArrayList();
        if (result.size() > 0) {
            list = result.into(ChoiceEntity.class);
        }
        return list;
    }

    public Long count(Condition cond) {
        Long count = dsl.selectCount().from(CHOICE).where(cond).fetchOneInto(Long.class);
        return count;
    }

    public Record getRecordById(Long id) {
        Record record = dsl.select().from(CHOICE).where(CHOICE.ID.eq(id)).fetchOne();
        return record;
    }

    public ChoiceEntity getEntityById(Long id) {
        Record r = getRecordById(id);
        if(r==null) {
            return null;
        }
        ChoiceEntity entity = r.into(ChoiceEntity.class);
        return entity;
    }

    public Long update(ChoiceEntity entity) {
        ChoiceRecord record = new ChoiceRecord();
        record.from(entity);
        dsl.executeUpdate(record);
        return Long.valueOf(entity.getId());
    }

    public String insert(ChoiceEntity entity) {
        ChoiceRecord record = new ChoiceRecord();
        if(StringUtils.isEmpty(entity.getId())) {
            String id = Helper.getSnowId() + "";
            entity.setId(id);
        }
        record.from(entity);
        dsl.executeInsert(record);
        return entity.getId();
    }

    public String save(ChoiceEntity entity) {
        if (entity.getId()==null||entity.getId().isEmpty()) {
            return insert(entity);
        }
        ChoiceEntity entity2 = getEntityById(Long.valueOf(entity.getId()));
        if(entity2==null) {
            return insert(entity);
        }
        update(entity);
        return entity.getId();
    }

    public void deleteById(Long id) {
        dsl.delete(CHOICE).where(CHOICE.ID.eq(id)).execute();
    }


}
